//获取账号密码登录节点
let username = document.querySelector('input[name="username"]')
let password = document.querySelector('input[name="password"]')
let dl = document.querySelector('input[name="dl"]')
let form = document.querySelector('form')

//账号非空，格式判定，封装成函数
function zh() {
    if (username.value == '') {
        username.nextElementSibling.innerText = '账号不能为空'
    } else {
        let reg = /^\d{9}$/
        if (reg.test(username.value) == true) {
            username.nextElementSibling.innerText = ''
            return 1 //验证成功返回一个值下面做判定是否提交
        } else {
            username.nextElementSibling.innerText = '请正确输入账号'
        }
    }
}
//账号失去焦点验证
username.onblur = function () {
    zh()
}
//密码非空，格式判定，封装成函数
function mima() {
    if (password.value == '') {
        password.nextElementSibling.innerText = '密码不能为空'
    } else {
        let reg = /^[a-z]\d{8}$/
        if (reg.test(password.value) == true) {
            password.nextElementSibling.innerText = ''
            return 1 //验证成功返回一个值下面做判定是否提交
        } else {
            password.nextElementSibling.innerText = '账号或密码错误'
        }

    }
}
//密码失去焦点验证
password.onblur = function () {
    mima()
}
//登录事件
form.onsubmit = function (e) {
    zh()
    mima()
    e = e || window.event

    if (zh() == 1 && mima() == 1) {
        let name = username.value
        let mima = password.value
        let obj = {
            name: name,
            mima: mima
        }
        let str = localStorage.getItem('yonghu')
        let arr = JSON.parse(str) || []
        // //有问题这段代码
        // let a = arr.indexOf(obj) //返回的索引一直都是-1，账号密码输入正确，从持久化数组里面去比较值返回索引时，返回不了，一直都是-1，是数据格式的问题
        // // console.log(a)
        let zhi = arr.some(function (item) {
            return item.name == obj.name && item.mima == obj.mima
            // return item == obj 一定不要这么写，必须把值取出来，上面才是对的(小本本记下来重点重点，这个位置卡了我一天了)
        })

        if (zhi == true) {
            location.href = `http://localhost:2202/index.html?username=${username.value}`
        } else {
            alert('账号未注册，你还是新手')
        }
    }
    return false
}